//
//  ExamRegistrationViewController.m
//  FH2go
//
//  Created by Hannes Feichtl on 11.04.13.
//  Copyright (c) 2013 Hannes Feichtl. All rights reserved.
//

#import "ExamRegistrationViewController.h"

@interface ExamRegistrationViewController ()

@end

@implementation ExamRegistrationViewController

@synthesize exam, label, registerButton, titleLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [registerButton setEnabled:YES];
    
    registrationManager = [[ExamRegistrationManager alloc] init];
    
    [self.titleLabel setBackgroundColor:[UIColor colorWithRed: 0.9 green: 0.9 blue: 0.9 alpha: 1.0]];
    titleLabel.text = [NSString stringWithFormat:@"    %@",exam.title];
    
    [self.label setBackgroundColor:[UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0]];
    label.text = [NSString stringWithFormat:@"    Date: %@\n    Type: %@",exam.dateString,exam.type];
    
    if([exam.status isEqual:@"registered"]) {
        [registerButton setTitle:@"Sign Off" forState:UIControlStateNormal];
    } else if([exam.status isEqual:@"notallowed"]) {
        [registerButton setTitle:@"Not allowed" forState:UIControlStateNormal];
        [registerButton setEnabled:NO];
    } else if([exam.identifier isEqual:@""] || [exam.identifier isEqual:@"DISABLED"]) {
        [registerButton setEnabled:NO];
        [registerButton setTitle:@"Not Allowed" forState:UIControlStateNormal];
        [registerButton setTitleColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1] forState:UIControlStateNormal];
    } else {
        [registerButton setTitle:@"Register" forState:UIControlStateNormal];
    }

}

-(IBAction)registerExam:(id)sender {
    
    UIActivityIndicatorView *loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    if(self.view.center.x > 300)
        [loadingIndicator setCenter:CGPointMake(self.view.center.y+180,self.view.center.x-100)];
    else
        [loadingIndicator setCenter:CGPointMake(self.view.center.x, self.view.center.y)];
    [loadingIndicator startAnimating];
    [self.view addSubview:loadingIndicator];
    
    [registerButton setEnabled:NO];
    
    [registrationManager registerExam:exam withHandler:^(NSError *error){
        if (!error) {
            
            UIViewController *prevVC = [self.navigationController.viewControllers objectAtIndex:1];
            [self.navigationController popToViewController:prevVC animated:YES];
            [prevVC viewDidLoad];

        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notice" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
